package com.example.smartcs.service.impl;

import com.example.smartcs.model.HumanIntervention;
import com.example.smartcs.service.HumanInterventionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * 人工干预服务实现类
 * 使用内存存储实现，实际项目中应该使用数据库
 */
@Slf4j
@Service
public class HumanInterventionServiceImpl implements HumanInterventionService {
    
    // 使用内存存储干预记录
    private final Map<String, HumanIntervention> interventions = new ConcurrentHashMap<>();
    private final Map<String, List<String>> sessionInterventions = new ConcurrentHashMap<>();
    private final Map<String, String> messageInterventions = new ConcurrentHashMap<>();
    
    @Override
    public HumanIntervention recordIntervention(HumanIntervention intervention) {
        // 设置ID和时间戳
        intervention.setId(UUID.randomUUID().toString());
        intervention.setTimestamp(LocalDateTime.now());
        
        // 保存干预记录
        interventions.put(intervention.getId(), intervention);
        
        // 更新会话索引
        sessionInterventions.computeIfAbsent(intervention.getSessionId(), k -> new ArrayList<>())
                .add(intervention.getId());
        
        // 更新消息索引
        if (intervention.getMessageId() != null) {
            messageInterventions.put(intervention.getMessageId(), intervention.getId());
        }
        
        log.info("记录人工干预: {}", intervention);
        return intervention;
    }
    
    @Override
    public List<HumanIntervention> getSessionInterventions(String sessionId) {
        List<String> interventionIds = sessionInterventions.getOrDefault(sessionId, new ArrayList<>());
        return interventionIds.stream()
                .map(interventions::get)
                .collect(Collectors.toList());
    }
    
    @Override
    public HumanIntervention getMessageIntervention(String messageId) {
        String interventionId = messageInterventions.get(messageId);
        return interventionId != null ? interventions.get(interventionId) : null;
    }
    
    @Override
    public HumanIntervention updateIntervention(HumanIntervention intervention) {
        if (!interventions.containsKey(intervention.getId())) {
            throw new IllegalArgumentException("干预记录不存在: " + intervention.getId());
        }
        
        intervention.setTimestamp(LocalDateTime.now());
        interventions.put(intervention.getId(), intervention);
        log.info("更新人工干预: {}", intervention);
        
        return intervention;
    }
    
    @Override
    public void deleteIntervention(String id) {
        HumanIntervention intervention = interventions.remove(id);
        if (intervention != null) {
            // 清理索引
            sessionInterventions.get(intervention.getSessionId()).remove(id);
            if (intervention.getMessageId() != null) {
                messageInterventions.remove(intervention.getMessageId());
            }
            log.info("删除人工干预: {}", id);
        }
    }
} 